package com.molichuxing.services.business.service.impl;

import com.molichuxing.services.business.dto.request.create.MessageCenterCreateBizDto;
import com.molichuxing.services.business.service.MessageCenterBizService;
import com.molichuxing.services.infrastructure.dto.request.create.MessageContentCreateDto;
import com.molichuxing.services.infrastructure.service.MessageCenterService;
import com.molichuxing.services.infrastructure.service.MessageContentService;
import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: renzhengyu
 * @Description:
 * @Date: Created in 14:36 2019/10/10
 * @Modified By:
 **/
@Service("messageCenterBizService")
public class MessageCenterBizServiceImpl implements MessageCenterBizService {
    private static final Logger log = Logger.getLogger(MessageCenterBizServiceImpl.class);

    @Autowired
    private MessageCenterService messageCenterService;

    @Autowired
    private MessageContentService messageContentService;

    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
    public Boolean careteMessageCenterBiz(MessageCenterCreateBizDto messageCenterCreateBizDto) throws Exception {
        if(null == messageCenterCreateBizDto){
            return false;
        }
        Integer centerId = messageCenterService.createMessageCenter(messageCenterCreateBizDto.getMessageCenterCreateDto());
        if(null == centerId ){
            throw new Exception("创建消息主体信息失败");
        }

        if(null != messageCenterCreateBizDto.getMessageContentCreateDtoList() && messageCenterCreateBizDto.getMessageContentCreateDtoList().size() > 0){
            List<MessageContentCreateDto> messageContentCreateDtoList = new ArrayList<>();
            MessageContentCreateDto messageContentCreateDto = null;
            for (MessageContentCreateDto dto: messageCenterCreateBizDto.getMessageContentCreateDtoList()) {
                messageContentCreateDto = new MessageContentCreateDto();
                BeanUtils.copyProperties(dto,messageContentCreateDto);
                messageContentCreateDto.setCenterId(centerId);
                messageContentCreateDtoList.add(messageContentCreateDto);
            }
            messageContentService.createMessageContentList(messageContentCreateDtoList);
        }
        return true;
    }
}
